package com.liuchaoya.tools;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class LMD5Util {
	public static String MD5Twice(String str) {//MD5两次
		return MD5(MD5(str));
	}

	public static String MD5(String passwd) {

		try {
			MessageDigest instance = MessageDigest.getInstance("MD5");
			byte[] digest = instance.digest(passwd.getBytes());

			StringBuilder sb = new StringBuilder();
			for (byte b : digest) {
				int i = b & 0xff;
				String hexString = Integer.toHexString(i);
				if (hexString.length() < 2) {
					hexString = "0" + hexString;
				}
				sb.append(hexString);
			}
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return "";
	}
	
	public static String sha1(String plainText) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA1");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}

			return buf.toString();


		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return "";
		}
	}
}
